//index.js
import {
  API_USERINFO_LIST,
  API_BANNER_LIST,
  API_TOURIST_TUBNBUNDLING
} from '../../api/index'

import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp()

Page({
  data: {
    userInfo: {}, // 微信用户数据
    userWenGanYunInfoList: [], // 温感云用户数据
    swiperImages: ['../../assets/images/banner.png'], // 轮播图图片路径
    swiperConfig: { // 轮播图配置
      indicatorDots: true, // 是否显示面板指示点
      autoplay: true, // 是否自动切换
      interval: 4000, // 自动切换时间间隔
      circular: true, // 是否采用衔接滑动
      duration: 500 // 滑动动画时长
    }
  },

  onLoad: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      userWenGanYunInfoList: wx.getStorageSync('userWenGanYunInfoList'),
      swiperImages: wx.getStorageSync('banners')
    })
  },

  onShow() {
    this.initData()
  },

  initData() {
    const paramsOpenId = {
      open_id: wx.getStorageSync('openId')
    }
    // 获取用户数据
    API_USERINFO_LIST(paramsOpenId).then(res => {
      // 设置用户数据
      wx.setStorageSync('userWenGanYunInfoList', res.data)
      this.setData({
        userWenGanYunInfoList: res.data,
      })
    })
    // 获取banner图
    API_BANNER_LIST(paramsOpenId).then(res => {
      // 设置banner数据
      wx.setStorageSync('banners', res.data)
      this.setData({
        swiperImages: res.data
      })
    })
  },

  // 解绑系统
  onUnDindSystem(e) {
    Dialog.confirm({
      title: '解绑提示',
      message: '将解绑该游客用户, 确认解绑?',
    })
      .then(() => {
        const params = {
          tpcode: e.currentTarget.dataset.item.tpcode, 
          open_id: wx.getStorageSync('openId')
        }
        API_TOURIST_TUBNBUNDLING(params).then(res => {
          if (res.code == 200) {
            wx.showToast({
              title: '解绑成功',
            })
            this.initData()
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  // 测温列表
  onGoTemperature(e) {
    wx.setStorageSync('bindFacilityData', e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/pages/temperature/temperature',
    })
  },

  // 绑定体温贴
  onGoFacility(e) {
    wx.setStorageSync('bindFacilityData', e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/pages/facility/facility',
    })
  },

  // 去绑定人员
  onGoBinding() {
    wx.navigateTo({
      url: '/pages/binding/binding',
    })
  }
})
